package com.mybatis.domain;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.StringJoiner;

public class Bean implements Serializable {
    public String id;
    public String name;

    public Bean(String id, String name) {
        this.id = id;
        this.name = name;
    }

    /**
     * 在序列化时重写对象
     */
    public final Object writeReplace() throws ObjectStreamException {
        return new Bean("111", "222");
    }

    /**
     * 在反序列化时转换对象
     */
    public final Object readResolve() throws ObjectStreamException {
        this.name = "333";
        return this;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Bean.class.getSimpleName() + "[", "]")
                .add("id='" + id + "'")
                .add("name='" + name + "'")
                .toString();
    }
}
